package string.leetcode.medium;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bruin_du
 * @description 不含重复字符的最长子字符串
 * @date 2022/8/4 18:54
 **/
public class OfferII016_LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) return 0;
        int res = 0, start = 0, len = s.length();
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            if (map.containsKey(ch)) {
                start = Math.max(start, map.get(ch) + 1);
            }
            res = Math.max(res, i - start + 1);
            map.put(ch, i);
        }
        return res;
    }
}
